package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

/**
 * @author zzw
 * @date 2025/5/18
 * @description 报表统计
 */
@Api(tags = "数据统计报表接口")
@RestController
@RequestMapping("/admin/report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 导出Excel报表
     */
    @ApiOperation("导出运营数据Excel报表")
    @GetMapping("/export")
    public void export(@ApiParam(value = "HTTP响应对象", required = true) HttpServletResponse response) {
        reportService.exportBusinessData(response);
    }

    /**
     * 获取销量排名top10
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return Result<SalesTop10ReportVO>
     */
    @ApiOperation("获取销量排名Top10")
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> getTop10(
            @ApiParam(value = "开始日期(yyyy-MM-dd)", required = true, example = "2025-01-01") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @ApiParam(value = "结束日期(yyyy-MM-dd)", required = true, example = "2025-12-31") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        SalesTop10ReportVO salesTop10ReportVO = reportService.getTop10(begin, end);
        return Result.success(salesTop10ReportVO);
    }

    /**
     * 获取用户统计数据
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return Result<UserReportVO>
     */
    @ApiOperation("获取用户统计数据")
    @GetMapping("/userStatistics")
    public Result<UserReportVO> getUserStatistics(
            @ApiParam(value = "开始日期(yyyy-MM-dd)", required = true, example = "2025-01-01") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @ApiParam(value = "结束日期(yyyy-MM-dd)", required = true, example = "2025-12-31") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        UserReportVO userReportVO = reportService.getUserStatistics(begin, end);
        return Result.success(userReportVO);
    }

    /**
     * 获取营业额统计数据
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return Result<TurnoverReportVO>
     */
    @ApiOperation("获取营业额统计数据")
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> getTurnoverStatistics(
            @ApiParam(value = "开始日期(yyyy-MM-dd)", required = true, example = "2025-01-01") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @ApiParam(value = "结束日期(yyyy-MM-dd)", required = true, example = "2025-12-31") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        TurnoverReportVO turnoverReportVO = reportService.getTurnoverStatistics(begin, end);
        return Result.success(turnoverReportVO);
    }

    /**
     * 获取订单统计数据
     *
     * @param begin 开始时间
     * @param end   结束时间
     * @return Result<OrderReportVO>
     */
    @ApiOperation("获取订单统计数据")
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> getOrderStatistics(
            @ApiParam(value = "开始日期(yyyy-MM-dd)", required = true, example = "2025-01-01") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @ApiParam(value = "结束日期(yyyy-MM-dd)", required = true, example = "2025-12-31") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        OrderReportVO orderReportVO = reportService.getReportOrderStatistics(begin, end);
        return Result.success(orderReportVO);
    }
}
